const http = require("http");
const fs = require("fs");
const path = require("path");
const argvArr = process.argv;

var fileWrite = argvArr[2];

http.createServer((rep, res) => {
   
    fs.access(__dirname + "/" + fileWrite, err => {
        if(err && fileWrite != null){
            res.writeHead(200, {"Content-type": "text/html; charset = utf-8"});
            res.end("Not Found!");
        }
        else{
            if(fileWrite != null){
                var filePath = path.join(__dirname, "/" + fileWrite);
            }
            else{
                var filePath = path.join(__dirname, "/fileReader1.js");
            }

            fs.readFile(filePath, (err ,data) => {
                if(err){
                    res.writeHead(200, {"Content-type": "text/html; charset = utf-8"});
                    res.end("err");
                }
                else{
                    res.writeHead(200, {"Content-type": "text/html; charset = utf-8"});
                    res.end(data);
                }
            })

        }
    })
}).listen(8081);

console.log("sever is listening 8081");
